package 网络;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Formatter;
import java.util.Locale;

public class Test {

    /**
     * @param args
     * @throws UnknownHostException
     * @throws SocketException
     */
    public static void main(String[] args) throws UnknownHostException,
            SocketException {
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address);
        //获得mac地址
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
        byte[] mac = networkInterface.getHardwareAddress();
        try (Formatter formatter = new Formatter()) {
            String MAC = null;
            for (int i = 0; i < mac.length; i++) {
                MAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], (i < mac.length - 1) ? "-" : "").toString();
            }
            System.out.println(MAC);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
